#!/bin/sh

set -o errexit

declare USER_NAME=$1
declare PASSWORD=$2
declare IP_PORT=$3
declare IP_PORT_LB=$4
declare REQ_GROUPS=$5

declare ERQ_KEY=$(pwd)/newclient.key""
declare REQ_CERT=$(pwd)/newclient.pem

declare REQ_URL_MENU=https://${IP_PORT}/app/rest.....
declare CREATE_GROUP_URL=https//${IP_PORT_LB}/v1/.....

declare header1="Accept: */*"
declare header2="Content-Type:application/json"


set_color(){
	export red='\033[0;31m'
	export green='\033[0;32m'
	export yellow='\033[0;33m'
	export plain='\033[0m'
}

function log_info(){
	echo -e "$(date +%F\ %T\.%N) [${green}INFO${plain}] $1" | tee -a $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/info.log
}

function log_warn(){
	echo -e "$(date +%F\ %T\.%N) [WARN] $1" | tee -a $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/info.log
}

function log_err(){
	echo -e "$(date +%F\ %T\.%N) [ERROR] $1" | tee -a $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/info.log
}


function call_create_groups(){
	log_info "Start to ........"
	local json_data=$REQ_GROUPS
	echo " curl -k --key $ERQ_KEY --cert $REQ_CERT -X POST \"${CREATE_GROUP_URL}\" -H \"${header1}\" -H \"${header2}\" -d '${json_data}' " > temp_req.log
	cat temp_req.log >> info.log
	source temp_req.log > resultCreateJson
	member_group_id=$(cat resultCreateJson | sed 's/,/\n/g' | grep "member_group_id" | sed 's/:/\n/g' | sed '1d' | sed 's/}//g' | sed -n '1p' | sed 's/\"//g' )
	cat resultCreateJson >> info.log
	echo >> info.log

	if [ -n "$member_group_id"];
	then
		log_info"...success"
		exit 0
	else
		log_err"....error"
		exit 1
	fi


}


function call_other(){
	if [ "$result"x == "true"x ];
	then
	    log_info "success...."
	else
		log_err "failed ...."
	fi
}


function check_input_params(){
	if [[ -n $USER_NAME]] && [[ -n $PASSWORD ]];then
		log_info ""
	else
		log_err ""
		exit 1
	fi
}

function main(){

   check_input_params
   call_create_groups
}

main


resultshell=$(bash xxx.sh)  #可以获取shell返回值
json_data=$(cat addmeus.json) # 读取json文件，赋值给变量